#include "TitleBar.h"

TitleBar::TitleBar(QWidget *parent) : QWidget(parent), m_maximized(false)
{
    setFixedHeight(30);
    setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    // 设置背景色和边框
    setStyleSheet("TitleBar { background: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
                  "stop:0 #5c5c5c, stop:1 #3d3d3d); "
                  "border-top-left-radius: 4px; border-top-right-radius: 4px; }");

    // 创建图标标签
    m_iconLabel = new QLabel(this);
    m_iconLabel->setFixedSize(20, 20);
    m_iconLabel->setScaledContents(true);
    m_iconLabel->setStyleSheet("QLabel { margin-left: 5px; }");

    // 创建标题标签
    m_titleLabel = new QLabel(this);
    m_titleLabel->setStyleSheet("QLabel { color: white; font: 10pt 'Arial'; margin-left: 5px; }");

    // 创建最小化按钮
    m_minimizeButton = new QPushButton(this);
    m_minimizeButton->setFixedSize(30, 30);
    m_minimizeButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarMinButton));
    m_minimizeButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #5c5c5c; }"
        "QPushButton:pressed { background: #7c7c7c; }");

    // 创建最大化按钮
    m_maximizeButton = new QPushButton(this);
    m_maximizeButton->setFixedSize(30, 30);
    m_maximizeButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarMaxButton));
    m_maximizeButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #5c5c5c; }"
        "QPushButton:pressed { background: #7c7c7c; }");

    // 创建还原按钮
    m_restoreButton = new QPushButton(this);
    m_restoreButton->setFixedSize(30, 30);
    m_restoreButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarNormalButton));
    m_restoreButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #5c5c5c; }"
        "QPushButton:pressed { background: #7c7c7c; }");
    m_restoreButton->setVisible(false);

    // 创建关闭按钮
    m_closeButton = new QPushButton(this);
    m_closeButton->setFixedSize(30, 30);
    m_closeButton->setIcon(QApplication::style()->standardIcon(QStyle::SP_TitleBarCloseButton));
    m_closeButton->setStyleSheet(
        "QPushButton { border: none; background: transparent; }"
        "QPushButton:hover { background: #e81123; }"
        "QPushButton:pressed { background: #f1707a; }");

    // 连接信号槽
    connect(m_minimizeButton, &QPushButton::clicked, this, &TitleBar::onClicked);
    connect(m_maximizeButton, &QPushButton::clicked, this, &TitleBar::onClicked);
    connect(m_restoreButton, &QPushButton::clicked, this, &TitleBar::onClicked);
    connect(m_closeButton, &QPushButton::clicked, this, &TitleBar::onClicked);

    // 设置布局
    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->setContentsMargins(0, 0, 0, 0);
    layout->setSpacing(0);
    layout->addWidget(m_iconLabel);
    layout->addWidget(m_titleLabel);
    layout->addStretch();
    layout->addWidget(m_minimizeButton);
    layout->addWidget(m_maximizeButton);
    layout->addWidget(m_restoreButton);
    layout->addWidget(m_closeButton);
}

TitleBar::~TitleBar()
{
}

void TitleBar::setWindowTitle(const QString &title)
{
    m_titleLabel->setText(title);
}

void TitleBar::setTitleBarIcon(const QIcon &icon)
{
    m_iconLabel->setPixmap(icon.pixmap(20, 20));
}

void TitleBar::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_startMovePos = event->pos();
    }
    QWidget::mousePressEvent(event);
}

void TitleBar::mouseMoveEvent(QMouseEvent *event)
{
    if (event->buttons() & Qt::LeftButton) {
        QWidget *parent = parentWidget();
        if (parent) {
            parent->move(parent->pos() + (event->pos() - m_startMovePos));
        }
    }
    QWidget::mouseMoveEvent(event);
}

void TitleBar::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        m_maximized = !m_maximized;
        updateMaximize();
        emit m_maximized ? m_maximizeButton->click() : m_restoreButton->click();
    }
    QWidget::mouseDoubleClickEvent(event);
}

void TitleBar::onClicked()
{
    QPushButton *button = qobject_cast<QPushButton *>(sender());
    QWidget *parent = parentWidget();

    if (!parent) return;

    if (button == m_minimizeButton) {
        parent->showMinimized();
    } else if (button == m_maximizeButton) {
        parent->showMaximized();
        m_maximized = true;
        updateMaximize();
    } else if (button == m_restoreButton) {
        parent->showNormal();
        m_maximized = false;
        updateMaximize();
    } else if (button == m_closeButton) {
        parent->close();
    }
}

void TitleBar::updateMaximize()
{
    m_maximizeButton->setVisible(!m_maximized);
    m_restoreButton->setVisible(m_maximized);
}
